<?php //$Id: user_bulk_confirm.php,v 1.3.2.1 2007/11/13 09:02:12 skodak Exp $
/**
* script for bulk user delete operations
*/

require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/dmllib.php');

$confirm = optional_param('confirm', 0, PARAM_BOOL);

admin_externalpage_setup('userbulk');
require_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));

$return = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';

if (empty($SESSION->bulk_users)) {
    redirect($return);
}

admin_externalpage_print_header();

//TODO: add support for large number of users

if ($confirm and confirm_sesskey()) {
    $in = implode(',', $SESSION->bulk_users);
    if ($rs = get_recordset_select('user', "id IN ($in)", '', 'id, username, suspended, firstname, lastname')) {
        while ($user = rs_fetch_next_record($rs)) {
            if (!$user->suspended) {
                continue;
            }
            //$auth = get_auth_plugin($user->auth);
            $result = user_suspended(($user->username), 0);
            if ($result != ENABLE_CONFIRM_OK && $result != ENABLE_CONFIRM_ALREADY) {
                notify(get_string('usernotconfirmed', '', fullname($user, true)));
            }
        }
        rs_close($rs);
    }
    redirect($return, get_string('changessaved'));

} else {
    $in = implode(',', $SESSION->bulk_users);
    $userlist = get_records_select_menu('user', "id IN ($in)", 'fullname', 'id,'.sql_fullname().' AS fullname');
    $usernames = implode(', ', $userlist);
    $optionsyes = array();
    $optionsyes['confirm'] = 1;
    $optionsyes['sesskey'] = sesskey();
    print_heading(get_string('confirmation', 'admin'));
    notice_yesno(get_string('confirmcheckfull', '', $usernames), 'user_unlock.php', 'user_bulk.php', $optionsyes, NULL, 'post', 'get');
}
/**
     * Confirm the new user as registered. This should normally not be used,
     * but it may be necessary if the user auth_method is changed to manual 
     * before the user is confirmed.
     */
     function user_suspended($username, $suspended) {        
        $user = get_complete_user_data('username', $username);

        if (!empty($user)) {
            if (!$user->suspended) {
                return ENABLE_CONFIRM_ALREADY;
            } else { 
                if (!set_field("user", "suspended", $suspended, "id", $user->id)) {
                    return ENABLE_CONFIRM_FAIL;
                }
                return ENABLE_CONFIRM_OK;
            }
        } else  {
            return ENABLE_CONFIRM_ERROR;
        }
    }
admin_externalpage_print_footer();
?>